package com.ruoyi.project.exam.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.QuType;
import com.ruoyi.common.utils.IdGen;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.exam.domain.ExamModel;
import com.ruoyi.project.exam.domain.ExamPaperModel;
import com.ruoyi.project.exam.domain.ExamRepoModel;
import com.ruoyi.project.exam.domain.PaperQuModel;
import com.ruoyi.project.exam.domain.QuModel;
import com.ruoyi.project.exam.domain.QuTypeCountModel;
import com.ruoyi.project.exam.mapper.ExamMapper;
import com.ruoyi.project.exam.mapper.ExamPaperMapper;
import com.ruoyi.project.exam.mapper.PaperQuMapper;
import com.ruoyi.project.exam.mapper.QuMapper;
import com.ruoyi.project.exam.service.IExamService;

@Service
public class ExamServiceImpl implements IExamService {

	@Autowired
	public ExamMapper examMapper;

	@Autowired
	private QuMapper quMapper;
	@Autowired
	private ExamPaperMapper examPaperMapper;

	@Autowired
	private PaperQuMapper  paperQuMapper;
	
	@Override
	public List<ExamModel> selectExamList(ExamModel model) {
		return examMapper.selectExamList(model);
	}

	@Override
	@Transactional
	public void insertExam(ExamModel model) {
		String id = IdGen.uuid();
		model.setId(id);
		model.setCreateBy(ShiroUtils.getLoginName());
		model.setCreateTime(new Date());
		List<ExamRepoModel> examRepoList = JSONObject.parseArray(model.getExamRepo(), ExamRepoModel.class);
		// 根据组卷规则及最大组卷套数生成试卷
		List<ExamPaperModel> examPapeList = new ArrayList<ExamPaperModel>();
		for (int i = 0; i < model.getPaperNum(); i++) {
			ExamPaperModel examPaperModel = new ExamPaperModel();
			examPaperModel.setId(IdGen.uuid());
			examPaperModel.setExamId(id);
			for (int j = 0; j < examRepoList.size(); j++) {
				ExamRepoModel examRepoModel = new ExamRepoModel();
				examRepoModel.setRepoId(examRepoList.get(j).getRepoId());
				if (examRepoList.get(j).getRadioCount() != null && examRepoList.get(j).getRadioCount() != 0) {
					QuTypeCountModel typeModel = new QuTypeCountModel(QuType.RADIO.getType(),
							examRepoList.get(j).getRadioCount(), examRepoList.get(j).getRadioScore());
					examRepoModel.getQuTypeCountList().add(typeModel);
				}
				if (examRepoList.get(j).getMultiCount() != null && examRepoList.get(j).getMultiCount() != 0) {
					QuTypeCountModel typeModel = new QuTypeCountModel(QuType.MULTI.getType(),
							examRepoList.get(j).getMultiCount(), examRepoList.get(j).getMultiScore());
					examRepoModel.getQuTypeCountList().add(typeModel);
				}
				if (examRepoList.get(j).getJudgeCount() != null && examRepoList.get(j).getJudgeCount() != 0) {
					QuTypeCountModel typeModel = new QuTypeCountModel(QuType.JUDGE.getType(),
							examRepoList.get(j).getJudgeCount(), examRepoList.get(j).getJudgeScore());
					examRepoModel.getQuTypeCountList().add(typeModel);
				}
				List<QuModel> quList = quMapper.selectQuListRandom(examRepoModel);
				examPaperModel.setQuList(quList);
			}
			if(examPaperModel.getQuList() != null && examPaperModel.getQuList().size() > 0){
				Collections.sort(examPaperModel.getQuList(), new Comparator<QuModel>(){
					@Override
					public int compare(QuModel o1, QuModel o2) {
						return Integer.parseInt(o1.getQuType())-Integer.parseInt(o2.getQuType());
					}
				});
			}
			examPapeList.add(examPaperModel);
		}
		examMapper.insertExam(model, examRepoList, examPapeList);
	}

	@Override
	public int deleteExamByIds(String ids) {
		return examMapper.deleteExamByIds(Convert.toStrArray(ids));
	}

	@Override
	public ExamModel selectExamById(String id) {
		return examMapper.selectExamById(id);
	}

	@Override
	@Transactional
	public void updateExam(ExamModel model) {
		model.setUpdateBy(ShiroUtils.getLoginName());
		model.setUpdateTime(new Date());
		examMapper.delExamRepoAndPaper(model.getId());
		List<ExamRepoModel> examRepoList = JSONObject.parseArray(model.getExamRepo(), ExamRepoModel.class);
		// 根据组卷规则及最大组卷套数生成试卷
		List<ExamPaperModel> examPapeList = new ArrayList<ExamPaperModel>();
		for (int i = 0; i < model.getPaperNum(); i++) {
			ExamPaperModel examPaperModel = new ExamPaperModel();
			examPaperModel.setId(IdGen.uuid());
			examPaperModel.setExamId(model.getId());
			for (int j = 0; j < examRepoList.size(); j++) {
				ExamRepoModel examRepoModel = new ExamRepoModel();
				examRepoModel.setRepoId(examRepoList.get(j).getRepoId());
				if (examRepoList.get(j).getRadioCount() != null && examRepoList.get(j).getRadioCount() != 0) {
					QuTypeCountModel typeModel = new QuTypeCountModel(QuType.RADIO.getType(),
							examRepoList.get(j).getRadioCount(), examRepoList.get(j).getRadioScore());
					examRepoModel.getQuTypeCountList().add(typeModel);
				}
				if (examRepoList.get(j).getMultiCount() != null && examRepoList.get(j).getMultiCount() != 0) {
					QuTypeCountModel typeModel = new QuTypeCountModel(QuType.MULTI.getType(),
							examRepoList.get(j).getMultiCount(), examRepoList.get(j).getMultiScore());
					examRepoModel.getQuTypeCountList().add(typeModel);
				}
				if (examRepoList.get(j).getJudgeCount() != null && examRepoList.get(j).getJudgeCount() != 0) {
					QuTypeCountModel typeModel = new QuTypeCountModel(QuType.JUDGE.getType(),
							examRepoList.get(j).getJudgeCount(), examRepoList.get(j).getJudgeScore());
					examRepoModel.getQuTypeCountList().add(typeModel);
				}
				List<QuModel> quList = quMapper.selectQuListRandom(examRepoModel);
				examPaperModel.setQuList(quList);
			}
			if(examPaperModel.getQuList() != null && examPaperModel.getQuList().size() > 0){
				Collections.sort(examPaperModel.getQuList(), new Comparator<QuModel>(){
					@Override
					public int compare(QuModel o1, QuModel o2) {
						return Integer.parseInt(o1.getQuType())-Integer.parseInt(o2.getQuType());
					}
				});
			}
			examPapeList.add(examPaperModel);
		}
		examMapper.updateExam(model, examRepoList, examPapeList);
	}

	@Override
	public int releaseExamById(String id) {
		return examMapper.releaseExamById(id);
	}

	@Override
	public List<ExamModel> selectExamOnlineList(ExamModel model) {
		return examMapper.selectExamOnlineList(model);
	}

	@Override
	public ExamModel selectUserExam(String id, Long userId) {
		return examMapper.selectUserExam(id, userId);
	}

	@Override
	@Transactional
	public ExamPaperModel selectExamPaperPre(String id) {
		ExamPaperModel model = examPaperMapper.selectExamPaperRandom(id);
		if(model != null){
			List<PaperQuModel> list = paperQuMapper.selectPaperQuByExamPaperId(model.getId());
			model.setPaperQulist(list);
		}
		return model;
	}

}
